package com.miao.shop.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Operation;
import com.miao.common.entity.pojo.Product;
import com.miao.common.entity.pojo.Store;
import com.miao.shop.service.OrderService;
import com.miao.shop.service.ShopOperationService;
import com.miao.shop.service.ShopProductService;
import com.miao.shop.service.ShopStoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商店表(Store)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-16 09:53:05
 */
@RestController
@RequestMapping("common/store")
public class ShopStoreController {
    /**
     * 服务对象
     */
    @Resource
    private ShopStoreService shopStoreService;
    @Resource
    private ShopProductService shopProductService;

    @Resource
    private ShopOperationService shopOperationService;


    @Resource
    private OrderService orderService;


    /**
     * 根据店铺Id 获取 店铺主页
     *
     * @param storeId 店铺id
     * @return R
     */
    @GetMapping("/index")
    public R storeIndex(String storeId) {
        Store store = this.shopStoreService.getById(storeId);
        if (null == store) {
            return R.error();
        }


        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("store_id", storeId);
        List<Product> products = this.shopProductService.list(productQueryWrapper);
        Map map = JSON.parseObject(store.getStoreIndex(), Map.class);
        HashMap<String, Object> storeVo = new HashMap<>();
        storeVo.put("storeDes", store.getStoreDes());
        storeVo.put("storeName", store.getStoreName());
        storeVo.put("storeId", String.valueOf(store.getStoreId()));
        if (null != map) {
            storeVo.put("storeIndex", map.get("html"));
        } else {
            storeVo.put("storeIndex", "<p style=\"text-align: center;\"><span style=\"font-size: 28px;\">店主很懒什么也没留下</span></p>");
        }

        storeVo.put("proList", products);
        return R.ok(storeVo);
    }


    @GetMapping("/get")
    public R storeInfo(String storeId) {
        Store store = this.shopStoreService.getById(storeId);
        if (null == store) {
            return R.error("店铺不存在");
        }

        HashMap<String, Object> storeVo = new HashMap<>();
        storeVo.put("storeDes", store.getStoreDes());
        storeVo.put("storeName", store.getStoreName());
        storeVo.put("storeId", String.valueOf(store.getStoreId()));
        storeVo.put("Rate",store.getStoreRate());

        return R.ok(storeVo);


    }


    /**
     * 店铺评价
     *
     * @param sId  店铺 id
     * @param oId  订单id
     * @param rate 评分
     * @return R
     */

    @GetMapping("/rate")
    public R storeRate(String sId, String oId, double rate) {

        this.orderService.updateRate(oId);
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.eq("store_id", sId);
        storeQueryWrapper.select("store_rate", "store_id");
        Store store = this.shopStoreService.getOne(storeQueryWrapper);
        DecimalFormat df = new DecimalFormat("#.00");
        store.setStoreRate(Double.valueOf(df.format((store.getStoreRate()+rate)/2)));

        Operation operation = new Operation();
        operation.setOperationDes("用户给出评分:"+rate);
        operation.setOrderId(oId);
        operation.setOperationTime(new Date());
        this.shopOperationService.save(operation);

        this.orderService.updateRate(oId);

        UpdateWrapper<Store> storeUpdateWrapper = new UpdateWrapper<>();
        storeUpdateWrapper.set("store_rate",store.getStoreRate());
        if (this.shopStoreService.update(store, storeUpdateWrapper)) {
            return R.ok();
        }
        return R.error();

    }


}
